import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import { getSentense } from '../../api/onSentense.js'
// 导入api函数，发送请求

const countSlice = createSlice({
    name: 'count',
    initialState: {
        num: 99
    },
    // 专门用来处理异步actionCreator修改数据的操作
    extraReducers: (builder) => {
        builder.addCase(asyncGetSentense.fulfilled,(state, {payload})=>{
            state.num += payload
        })
    }
})

// 创建异步的actionCreator

export const asyncGetSentense = createAsyncThunk('count/getSentense',async (payload)=>{
    // 发送ajax请求，获取响应结果，并求出句子长度，返回给extraReducer使用
    let res = await getSentense()
    return res.length; // 返回的是promise对象，成功的结果值是 响应结果字符串的长度
})


export default countSlice.reducer
